<?php

class ServiceSearch extends Service
{
	public $minRequestDate;
	public $maxRequestDate;
	public $minPurchaseDate;
	public $maxPurchaseDate;
	public $productType;
	
	public function rules()
	{
		$parentRules = parent::rules();
		return CMap::mergeArray($parentRules, array(
			array( 'minRequestDate, maxRequestDate, minPurchaseDate, maxPurchaseDate, productType', 'safe', 'on'=>'search'),
		));
	}
	
	public function attributeLabels()
	{
		$parentLabels = parent::attributeLabels();
		return CMap::mergeArray($parentLabels, array(
			'minRequestDate'=>'Request Date After',
			'maxRequestDate'=>'Request Date Before',
			'minPurchaseDate'=>'Purchase Date After',
			'maxPurchaseDate'=>'Purchae Date Before',
			'productType'=>'Product Type',
		));
	}
	
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		//$criteria->compare('date',$this->date,true);
		//$criteria->compare('purchaseDate',$this->purchaseDate,true);
		if(!empty($this->minRequestDate)){
			$criteria->compare('date','>='.$this->minRequestDate.' 00:00:00');
		}
		if(!empty($this->maxRequestDate)){
			$criteria->compare('date','<='.$this->maxRequestDate.' 23:59:59');
		}
		if(!empty($this->minPurchaseDate)){
			$criteria->compare('purchaseDate','>='.$this->minPurchaseDate);
		}
		if(!empty($this->maxPurchaseDate)){
			$criteria->compare('purchaseDate','<='.$this->maxPurchaseDate);
		}
		
		if(!empty($this->productType)){
			$criteria->with = array(
				'product'=>array(
					'condition'=>'type = :type',
					'params'=>array(':type'=>$this->productType),
				),
			);
		}
		
		$criteria->compare('technicianId',$this->technicianId);
		$criteria->compare('firstName',$this->firstName,true);
		$criteria->compare('LastName',$this->LastName,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('suburb',$this->suburb,true);
		$criteria->compare('postcode',$this->postcode,true);
		$criteria->compare('state',$this->state,true);
		$criteria->compare('homePhone',$this->homePhone,true);
		$criteria->compare('workPhone',$this->workPhone,true);
		$criteria->compare('mobile',$this->mobile,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('stairs',$this->stairs);
		$criteria->compare('productId',$this->productId);
		$criteria->compare('serialNo',$this->serialNo,true);
		$criteria->compare('generalComment',$this->generalComment,true);
		$criteria->compare('partComment',$this->partComment,true);
		$criteria->compare('warranty',$this->warranty);
		$criteria->compare('faultDescription',$this->faultDescription,true);
		$criteria->compare('customerAccountNo',$this->customerAccountNo,true);
		$criteria->compare('storeTinNo',$this->storeTinNo,true);
		$criteria->compare('otherInstruction',$this->otherInstruction,true);
		$criteria->compare('dateCommenced',$this->dateCommenced,true);
		$criteria->compare('dateCompleted',$this->dateCompleted,true);
		$criteria->compare('chargeApplicable',$this->chargeApplicable);
		$criteria->compare('customerCharge',$this->customerCharge,true);
		$criteria->compare('fundsRecovered',$this->fundsRecovered);
		$criteria->compare('paymentType',$this->paymentType);
		$criteria->compare('technicainPaid',$this->technicainPaid);
		$criteria->compare('technicainPaidAmount',$this->technicainPaidAmount,true);
		$criteria->compare('workCarriedOut',$this->workCarriedOut);
		$criteria->compare('note',$this->note,true);
		$criteria->compare('storeId',$this->storeId);
		$criteria->compare('emailNote',$this->emailNote,true);
		$criteria->compare('status',$this->status);
		
		if(!Yii::app()->request->isAjaxRequest)
			$criteria->order='id DESC';

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
			'pagination'=>array('pageSize'=>100),
		));
	}
}